package cn.sharp.bbs.controller;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.page.PageParam;
import cn.sharp.bbs.entity.BO.*;
import cn.sharp.bbs.service.BillService;
import cn.sharp.bbs.service.CommonService;
import cn.sharp.bbs.service.ScoreService;
import cn.sharp.bbs.service.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @className: BillController
 * @description: 积分相关控制类
 * @author: 王晓崧
 * @date: 2022/7/9
 **/

@RestController
@RequestMapping("/score")
public class ScoreController {

    @Resource
    private BillService billService;

    @Resource
    private TaskService taskService;

    @Resource
    private ScoreService scoreService;

    @Resource
    private CommonService commonService;

    /**
     * 获得当前用户的积分收入账单
     *
     * @param currentUser 发送请求的用户
     */
    @GetMapping("/bill/in")
    public Result getInBill(@RequestAttribute("CurrentUser") CurrentUser currentUser, BillIn billIn) {
        billIn.setU_id(currentUser.getU_id());
        return Result.success(billService.getInBill( billIn));
    }

    /**
     * 获得当前用户的积分支出账单
     *
     * @param currentUser 发送请求的用户
     */
    @GetMapping("/bill/out")
    public Result getOutBill(@RequestAttribute("CurrentUser") CurrentUser currentUser, BillOut billOut) {
        billOut.setU_id(currentUser.getU_id());
        return Result.success(billService.getOutBill( billOut));
    }


    /**
     * 创建积分任务
     *
     * @param task        前端传到后端的任务实体
     * @param currentUser 发送请求的用户
     */
    @PostMapping("/task/create")
    public Result createTask(@RequestBody TaskEntity task, @RequestAttribute("CurrentUser") CurrentUser currentUser) {
//        if (taskService.checkIsAdministrator(currentUser.getU_id())) {
        taskService.createTask(task, currentUser.getU_id());
        System.out.println(task);
        return Result.success();
//        } else {
//            return Result.fail(Result.ERR_CODE_BUSINESS, "你不是管理员，无法进行任务创建操作");
//        }
    }

    /**
     * 获取任务类型列表
     */
    @GetMapping("/task/typeList")
    public Result getTaskTypeList() {
        return Result.success(taskService.getTaskTypeList());
    }

    /**
     * 获得任务列表
     *
     * @param currentUser 发送请求的用户
     */
    @GetMapping("/task/taskList")
    public Result getTaskList(@RequestAttribute("CurrentUser") CurrentUser currentUser) {
//        if (taskService.checkIsAdministrator(currentUser.getU_id())) {//检查是否是管理员
        return Result.success(taskService.getTaskList());
//        } else {
//            return Result.fail(Result.ERR_CODE_BUSINESS, "你不是管理员，无法进行任务列表获取操作");
//        }
    }

    /**
     * 获取任务进度列表
     *
     * @param currentUser 发送请求的用户
     */
    @GetMapping("/task/rate")
    public Result getTaskRate(@RequestAttribute("CurrentUser") CurrentUser currentUser) {
        return Result.success(taskService.getRate(currentUser.getU_id()));
    }

    /**
     * 获得积分内容
     *
     * @param scoreEntity 包括了帖子id
     * @param currentUser 发送请求的用户
     */
    @GetMapping("/integralContent")
    public Result getIntegralContent(ScoreEntity scoreEntity, @RequestAttribute("CurrentUser") CurrentUser currentUser) {
        return Result.success(scoreService.getIntegralContent(scoreEntity.getPostID(), currentUser.getU_id()));
    }


    @GetMapping("/checkHasCont")
    public Result checkHasCont(@RequestBody ScoreEntity scoreEntity){
        return Result.success(scoreService.checkHasCont(scoreEntity.getPostID()));
    }
    /**
     * 解锁积分内容
     *
     * @param scoreEntity 包括了解锁帖子id
     * @param currentUser 发送请求的用户
     */
    @PostMapping("/unlock")
    public Result unlockContent(@RequestBody ScoreEntity scoreEntity, @RequestAttribute("CurrentUser") CurrentUser currentUser) {
        if (!scoreService.checkOwner(currentUser.getU_id(), scoreEntity.getPostID())) {//检查是否为帖子所有者
            if (!scoreService.checkUnlock(currentUser.getU_id(), scoreEntity.getPostID())) {//检查是否已经解锁过帖子积分内容
                if (scoreService.checkIsEnough(currentUser.getU_id(), scoreEntity.getPostID())) {//检查用户积分是否足够解锁
                    scoreService.unlockContent(currentUser.getU_id(), scoreEntity.getPostID());
                    return Result.success();
                } else {
                    return Result.fail(Result.ERR_CODE_BUSINESS, "积分不足！");
                }
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "已解锁过此内容！");
            }
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "你是帖子所有者，无需解锁！");
        }
    }


    /**
     * 检查帖子悬赏是否已被完成
     *
     * @param scoreEntity 前后端交互类
     */
    @GetMapping("/checkIsComplete")
    public Result checkIsComplete(ScoreEntity scoreEntity) {
        return Result.success(scoreService.checkCompleted(scoreEntity.getPostID()));
    }

    /**
     * 悬赏完成
     *
     * @param scoreEntity 包括了要完成悬赏的帖子id和完成悬赏的用户id
     * @param currentUser 发送请求的用户
     */
    @PostMapping("/complete")
    public Result completeReward(@RequestBody ScoreEntity scoreEntity, @RequestAttribute("CurrentUser") CurrentUser currentUser) {
        if (scoreService.checkOwner(currentUser.getU_id(), scoreEntity.getPostID())) {//检查是否为帖子所有者
            if (!scoreService.checkCompleted(scoreEntity.getPostID())) {//检查悬赏是否已完成
                if (scoreService.checkIsEnough(currentUser.getU_id(), scoreEntity.getPostID())) {//检查用户积分是否足够给予完成悬赏者积分
                    scoreService.completeReward(scoreEntity, currentUser.getU_id());
                    return Result.success();
                } else {
                    return Result.fail(Result.ERR_CODE_BUSINESS, "积分不足！");
                }
            } else {
                return Result.fail(Result.ERR_CODE_BUSINESS, "该悬赏已完成，无法多次完成！");
            }
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "你不是帖子所有者，无法进行悬赏完成操作！");
        }
    }

    /**
     * 获得帖子打赏列表
     *
     * @param scoreEntity 包括了需要获得的帖子的id
     */
    @GetMapping("/rewardList")
    public Result getRewardList(ScoreEntity scoreEntity) {
        return Result.success(scoreService.getRewardList(scoreEntity.getPostID()));
    }

    /**
     * 打赏帖子
     *
     * @param scoreEntity 包括打赏帖子id，打赏额度，打赏备注
     * @param currentUser 发送请求的用户
     */
    @PostMapping("/reward")
    public Result rewardPost(@RequestBody ScoreEntity scoreEntity, @RequestAttribute("CurrentUser") CurrentUser currentUser) {
        if (scoreService.checkIsEnough(currentUser.getU_id(), scoreEntity.getNum())) {//检查用户积分是否打赏
            scoreService.rewardPost(currentUser.getU_id(), scoreEntity);
            return Result.success();
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "积分不足！");
        }
    }

    /**
     * 签到
     *
     * @param currentUser 发送请求的用户
     */
    @PostMapping("/signIn")
    public Result signIn(@RequestAttribute("CurrentUser") CurrentUser currentUser) {
        if (!taskService.checkIsSignIn(currentUser.getU_id())) {//检查今日是否已经签到
            taskService.signIn(currentUser.getU_id());
            return Result.success();
        } else {
            return Result.fail(Result.ERR_CODE_BUSINESS, "今日已签到，无法重复签到！");
        }
    }
}
